<?php
/**
 * Minhlog Domain_Model 文章内容类
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 *
 * @category   Domain
 * @package    Domain_Model
 * @copyright  Copyright (c) 2009-2010 Minh.Shu@gmail.com
 * @license    New BSD License
 * @author     Minh.Shu
 * @version    $Id: Article.php 2 2010-01-17 13:49:08Z Minh.Shu $
 *
 */

class Domain_Model_Article
{
    private $_articleDataMapper;
    private $_articleEntity;
    private $_categoryDataMapper;
    private $_categoryEntity;
    private $_tagDataMapper;
    private $_tagEntity;

    public function  __construct()
    {
        $this->_articleDataMapper = new Domain_DataMapper_Article();
        $this->_articleEntity = new Domain_Entity_Article();
        $this->_articleDataMapper->setEntity($this->_articleEntity);
        $this->_categoryDataMapper = new Domain_DataMapper_Category();
        $this->_categoryEntity = new Domain_Entity_Category();
        $this->_categoryDataMapper->setEntity($this->_categoryEntity);
        $this->_tagDataMapper = new Domain_DataMapper_Tag();
        $this->_tagEntity = new Domain_Entity_Tag();
        $this->_tagDataMapper->setEntity($this->_tagEntity);
    }

    /**
     * 根据文章ID查询数据
     * @param integer $articleId
     * @return object|void
     */
    public function getArticleById($articleId)
    {
        $articleId = (int) $articleId;

        $articleRes = $this->_articleDataMapper->findArticleById($articleId);

        if(null === $articleRes){
            return ;
        }

        $category = $this->_categoryDataMapper->findCategoryByArticle($articleRes);
        $tagSet = $this->_tagDataMapper->findTagByArticle($articleRes);

        if(null !== $articleRes){
            $info = $this->_articleDataMapper->mapper($articleRes);
        }

        $article = new stdClass();
        $article->info = $info;
        $article->category = $category;
        $article->tags = $tagSet;
        return $article;
    }

    /**
     * 查询全部文章数据
     * @param object|void $where
     * @return object|void
     */
    public function getAllArticle($where = null)
    {
        $articleSet = $this->_articleDataMapper->findAllArticle($where);
        $categorySet = $this->_categoryDataMapper->findCategoryByArticles($articleSet);
        $tagSet = $this->_tagDataMapper->findTagByArticles($articleSet);

        $resultSet = array();

        if($articleSet->count()){
            foreach ($articleSet as $oneArticle) {
                $article =$this->_articleDataMapper->mapper($oneArticle);
                $articleObj = new stdClass();
                $articleObj->article = $article;
                if(array_key_exists($oneArticle->article_id, $categorySet)){
                    $articleObj->category = $categorySet[$oneArticle->article_id];
                }
                if(array_key_exists($oneArticle->article_id, $tagSet)){
                    $articleObj->tags = $tagSet[$oneArticle->article_id];
                }
                $resultSet[] = $articleObj;
            }
        }else{
            $resultSet = null;
        }

        return $resultSet;
    }

    /**
     * 根据分类查询文章数据
     * @param integer $categoryId
     * @param Zend_Db_Table_Rowset $rowset
     * @return object|void
     */
    public function getArticleByCategory($categoryId, Zend_Db_Table_Rowset $rowset)
    {
        $categoryIds = array($categoryId);
        foreach($rowset as $row){
            $categoryIds[] = $row->category_id;
        }
        $value = implode(',', $categoryIds);

        $where = new stdClass();
        $where->type = 'category';
        $where->value = $value;
        return $this->getAllArticle($where);
    }

    /**
     * 根据标签查询文章数据
     * @param Zend_Db_Table_Rowset $rowset
     * @return object|void
     */
    public function getArticleByTag(Zend_Db_Table_Rowset $rowset)
    {
        $articleIds = array();
        foreach($rowset as $row){
            $articleIds[] = $row->article_id;
        }
        $value = implode(',', $articleIds);
        $where = new stdClass();
        $where->type = 'tag';
        $where->value = $value;
        return $this->getAllArticle($where);
    }

    /**
     * 根据日期归档查询文章
     * @param interger $year
     * @param interger $month
     * @return object|void
     */
    public function getArticleByArchive($year, $month)
    {
        $filter = new Zend_Filter_Int();
        $year  = $filter->filter($year);
        $month = $filter->filter($month);

        $month = max(1,min(12, $month));
        $from = mktime(0, 0, 0, $month, 1, $year);
        $to   = mktime(0, 0, 0, $month+1 ,1 ,$year);
        $where = new stdClass();
        $where->type = 'archive';
        $where->value = array('from'=>$from, 'to'=>$to);
        return $this->getAllArticle($where);
    }

    /**
     * 根据发布时间查询文章数据
     * @param integer $num
     * @return object|void
     */
    public function getArticleByTime($num = 8)
    {
        $num = (int) $num;
        $resultSet = $this->_articleDataMapper->findArticleByTime($num);
        return $resultSet;
    }

    /**
     * 根据点击数查询文章数据
     * @param integer $num
     * @return object|void
     */
    public function getArticleByClick($num = 8)
    {
        $num = (int) $num;
        $resultSet = $this->_articleDataMapper->findArticleByClick($num);
        return $resultSet;
    }

    /**
     * 保存文章信息
     * @param array $data
     * @param integer primaryId
     * @return
     */
    public function saveArticle($data, $primaryId = 0)
    {
        $primaryId = (int) $primaryId;
        $resultSet = $this->_articleDataMapper->save($data, $primaryId);
        return $resultSet;
    }

    /**
     * 删除文章以及关联Tag关系
     * @param integer $primaryId
     * @return
     */
    public function delArticle($primaryId)
    {
        $primaryId = (int) $primaryId;
        return $this->_articleDataMapper->deleteDependent($primaryId);
    }
}